package org.khd.gift.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.khd.gift.clients.AuthClient;
import org.khd.gift.domain.LoginUser;
import org.khd.gift.domain.User;
import org.khd.gift.domain.UserInfo;
import org.khd.gift.dto.UserPhoneRegisterDTO;
import org.khd.gift.exception.AssertException;
import org.khd.gift.exception.ResponseCode;
import org.khd.gift.mapper.UserMapper;
import org.khd.gift.service.IUserInfoService;
import org.khd.gift.service.IUserService;
import org.khd.gift.service.IVerifyCodeService;
import org.khd.gift.util.AjaxResult;
import org.khd.gift.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author Saber
 * @since 2024-07-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private AuthClient authClient;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void registerPhone(UserPhoneRegisterDTO userPhoneRegisterDTO) {

        //1 参数校验
        //1.1 手机验证码校验
        //1.1.1 根据手机号去redis中获取验证码
        String phoneByRedis = verifyCodeService.getSmsCode(userPhoneRegisterDTO.getPhone());

        //1.1.2 再将前端传过来的验证码和redis中存放的验证码做比较
        AssertException.isEq(phoneByRedis, userPhoneRegisterDTO.getVerifyCode(), ResponseCode.RESPONSE_CODE_10019);

        //2 添加数据到数据库

        //密码加密加盐处理
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        String password = MD5.getMD5("rongligou" + salt + userPhoneRegisterDTO.getPassword());

        //2.1 远程调用授权中心，保存数据到t_login_user表，获取id
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername("rongligou" + UUID.randomUUID().toString().replaceAll("-", ""));
        loginUser.setSalt(salt);
        loginUser.setPassword(password);
        loginUser.setPhone(userPhoneRegisterDTO.getPhone());
        loginUser.setType(1);

        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        //如果远程调用失败，则抛出异常，注册失败
        AssertException.isTrue(ajaxResult.isSuccess(),ResponseCode.RESPONSE_CODE_10026);

        //如果远程调用成功，则获取loginUser的id
        Long loginId = Long.parseLong(ajaxResult.getResultObj().toString());

        //2.2 根据获取到的login_id添加数据到t_user
        User user = new User();
        user.setCreateTime(new Date().getTime());
        user.setPhone(userPhoneRegisterDTO.getPhone());
        user.setSalt(salt);
        user.setPassword(password);
        user.setBitState(1L);
        user.setLoginId(loginId);

        super.insert(user);

        //2.3 添加数据到t_user_info

        UserInfo userInfo = new UserInfo();
        userInfo.setCreateTime(new Date().getTime());
        userInfo.setRegTime(userInfo.getCreateTime());
        userInfo.setLevel(0);
        userInfo.setGrowScore(0);
        userInfo.setUserId(user.getId());
        userInfoService.insert(userInfo);

        //3 发送短信/邮件通知用户注册成功，使用mq异步发送，消息队列



    }
}
